package dhee.wtqshopproject.test;

import dhee.wtqshopproject.entity.News;
import dhee.wtqshopproject.utils.NewsMapperUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@Transactional
public class newstest {

    @Autowired
    private NewsMapperUtils newsMapperUtils;

    // 测试新增新闻
    @Test
    public void testInsertAndSelect() {
        // 准备测试数据
        News newNews = createTestNews();

        // 执行插入
        NewsMapperUtils.insertNews(newNews);

        // 验证查询
        List<News> newsList = NewsMapperUtils.selectAllNews();

        System.out.println("newsList = " + newsList);
    }

    // 测试删除新闻
    @Test
    public void testDeleteNews() {
        // 准备测试数据
        News news = createTestNews();
        NewsMapperUtils.insertNews(news);
        System.out.println(NewsMapperUtils.selectNewsById(news.getNewsId()));
        // 执行删除
        NewsMapperUtils.deleteNews(news.getNewsId());
        // 验证删除
        News deletedNews = NewsMapperUtils.selectNewsById(news.getNewsId());
        System.out.println("如果为空则删除成 " + deletedNews);
    }

    // 测试更新新闻
    @Test
    public void testUpdateNews() {
        // 准备数据
        News originalNews = createTestNews();
        NewsMapperUtils.insertNews(originalNews);

        // 修改数据
        originalNews.setTitle("更新后的标题");
        originalNews.setContent("更新后的内容");
        NewsMapperUtils.updateNews(originalNews);

        // 验证更新
        News updatedNews = NewsMapperUtils.selectNewsById(originalNews.getNewsId());

        System.out.println("updatedNews = " + updatedNews);

    }

    // 测试查询所有新闻（按时间倒序）
    @Test
    public void testSelectAllNews() {
        // 插入两条测试数据
        News news1 = createTestNews();
        News news2 = createTestNews();
        news2.setTitle("第二条新闻");
        NewsMapperUtils.insertNews(news1);
        NewsMapperUtils.insertNews(news2);

        // 获取排序结果
        List<News> newsList = NewsMapperUtils.selectAllNews();

        System.out.println("newsList = " + newsList);
    }



    // 创建测试用News对象
    private News createTestNews() {
        News news = new News();
        news.setTitle("测试新闻标题");
        news.setContent("测试新闻内容");
        news.setAdminId(1);
        return news;
    }
}